package jp.sourceforge.akjrcp.util;

import java.io.File;
import java.io.IOException;

public class FLVTool2Executer {
	private String flvtool2Path;
	private File input;
	private ProcessPrinter printer;

	
	public FLVTool2Executer(String flvtool2Path,ProcessPrinter printer){
		this.flvtool2Path=flvtool2Path;
		this.printer=printer;
	}
	
	public String[] toCommands(){
		String args[];
		if (flvtool2Path.indexOf("akj_friends") != -1) {
			args = new String[] { flvtool2Path, "-U",
					input.getAbsolutePath() };
		} else {
			args = new String[] { flvtool2Path, "u",
					input.getAbsolutePath() };
		}
		return args;
	}
	
	public void execute() throws IOException{
		Runtime runtime=Runtime.getRuntime();
		String commands[]=toCommands();
		
		printer.printOutput(toDebugArray(commands));
		
		Process process=runtime.exec(commands);
		ProcessMonitor monitor=new ProcessMonitor(process,printer);
		monitor.startMonitor();
		
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public String toDebugArray(String[] array){
		String v="";
		for (int i = 0; i < array.length; i++) {
			v+="\""+array[i]+"\""+" ";
		}
		return v+"\n";
	}

	public File getInput() {
		return input;
	}

	public void setInput(File input) {
		this.input = input;
	}
	
}
